package com.cvmars.jianyou.module.activity;

import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cvmars.jianyou.R;
import com.cvmars.jianyou.api.api.Api;
import com.cvmars.jianyou.api.api.HttpUtils;
import com.cvmars.jianyou.api.api.SimpleSubscriber;
import com.cvmars.jianyou.api.model.HttpResult;
import com.cvmars.jianyou.module.adapter.FindTopicAdapter;
import com.cvmars.jianyou.module.base.BaseActivity;
import com.cvmars.jianyou.module.model.FindListModel;
import com.cvmars.jianyou.module.model.FindModel;
import com.cvmars.jianyou.module.model.TopicListModel;
import com.cvmars.jianyou.module.model.TopicModel;
import com.cvmars.jianyou.ui.pulltorefresh.PulltoRefreshRecyclerView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class FindTopicSelectActivity extends BaseActivity {

    PulltoRefreshRecyclerView listHome;

    FindTopicAdapter homeAdapter;

    boolean isHot;

    List<TopicModel> modelList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_topic_select);


        listHome = findViewById(R.id.list_home);

        homeAdapter = new FindTopicAdapter(this, R.layout.item_find_topic, modelList);

        homeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = getIntent();
                intent.putExtra("topic", modelList.get(position).name);
                setResult(RESULT_OK, intent);
                finish();
//

            }
        });

        listHome.setAdapter(homeAdapter);
        listHome.setPullRefreshListener(new PulltoRefreshRecyclerView.RecyPtrHandler() {
            @Override
            public void onLoadMore() {

                requestNew();
            }

            @Override
            public void onRefresh() {

                listHome.mCurPager = 1;
                requestNew();

            }
        });


        requestNew();

    }

    private void requestNew() {


        HttpUtils.getInstance().toSubscribe(Api.getInstance().getTopicList()
                , new SimpleSubscriber<HttpResult<TopicListModel>>() {
                    @Override
                    protected void _onNext(HttpResult<TopicListModel> o) {

                        getLoadDialogAndDismiss();

                        TopicListModel data = o.getData();

                        if (data != null) {

                            List<TopicModel> results = data.results;

                            if (results != null) {

                                if (listHome.mCurPager == 1) {
                                    modelList.clear();
                                }
                                modelList.addAll(results);
                                homeAdapter.notifyDataSetChanged();
                                listHome.refreshComplete();
                                listHome.loadMoreComplete();

                            }
                            listHome.loadMoreEnd();
                        }
                    }

                    @Override
                    protected void _onError(String message) {

                    }
                });

    }
}
